<?php

/**
 * @filename MemberImport.php
 * @author qiankun<qiankun@dodoca.com>
 * @datetime 2017/4/12
 * @description 会员导入自动检查会员数据是否合格
 */

class MemberImport
{
    public static function init()
    {
        $memberImportModel = new Statistics_MemberImportModel();
        $memberImportFileModel = new Statistics_MemberImportFileModel();
        $file = $memberImportFileModel->find(array('status' => 1),array('id','file_id'),'id ASC');
        if($file)
        {
            //差错处理机制，删除无内容的excel数据防止死循环
            $where = array('import_file_id' => $file['id']);
            if($id = $memberImportModel->find($where, array('id')))
            {
                $memberImportFileModel->deleteData(array('id' => $id));
            }
            //差错处理end
            $where = array(
                'status' => 1, 'import_file_id' => $file['id']
            );
            $limit = 500;//单次执行条数

            $list = $memberImportModel->select($where, array('id', 'phone', 'level', 'balance', 'true_name', 'sex', 'birthday', 'card_no', 'seller_id','shop_id'), $limit);
            if(!empty($list))
            {
                foreach($list as $key => $value)
                {
                    $updateData = array();
                    $data = array(
                        'phone' => $value['phone'],
                        'shop_id' => $value['shop_id'],
                        'seller_id' => $value['seller_id'],
                        'level' => $value['level'],
                        'balance' => $value['balance'],
                        'true_name' => $value['true_name'],
                        'sex' => $value['sex'],
                        'birthday' => $value['birthday'],
                        'card_no' => $value['card_no'],
                    );
                    if(!in_array($data['sex'], array(0, 1, 2)))
                    {
                        $updateData['sex'] = 0;
                    }

                    if(!empty($data['card_no']))//判断卡号是否重复,重复设为空
                    {
                        $where = array(
                            'seller_id' => $value['seller_id'],
                            'shop_id' => $value['shop_id'],
                            'status' => array('in', '2,4'),
                            'card_no' => $data['card_no']
                        );
                        if($memberImportModel->count($where))
                        {
                            $updateData['card_no'] = '';
                        }
                    }
                    $isValidate = Member_ImportService::checkMemberImport($data);
                    if($isValidate['status'] == 0)
                    {
                        $updateData['status'] = 3;//不合格
                        $updateData['error_msg'] = $isValidate['msg'];
                    }
                    elseif($isValidate['status'] == 1)
                    {
                        $updateData['status'] = 2;//合格
                    }
                    $where = array('id' => $value['id']);

                    $memberImportModel->updateData($updateData, $where);//修改合格状态
                    Tool_Log::info('更新成功的member_import:ID', 'memberImport', array('method' => 'MemberImport：init', 'msg' => '更新结果', 'result' => $value['id']));
                }
            }
            else//如果检查完
            {
                $data = array('status' => 2);
                $where = array(
                    'id' => $file['id'],
                );
                $memberImportFileModel->updateData($data,$where);//更新FILE表状态为已完成检查
                Tool_Log::info('检查完的member_import_file:ID', 'memberImport', array('method' => 'MemberImport：init', 'msg' => '更新结果', 'result' => $where));
            }

        }

    }
}